#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void exchg1(int x, int y){
    int tmp;
    tmp = x;
    x = y;
    y = tmp;
    std::cout << "函数内交换后: x=" << x <<", y=" << y << std::endl;
}


void exchg2(int *px, int *py){
    int tmp;
    tmp = *px;
    *px = *py;
    *py = tmp;
    std::cout << "函数内交换后: *px=" << *px <<", *py=" << *py << std::endl;
}
void exchg3(int &x, int &y){
    int tmp;
    tmp = x;
    x = y;
    y = tmp;
    std::cout << "函数内交换后: x=" << x <<", y=" << y << std::endl;
}


void getMemory(char *p, int num){
    p =(char *)malloc(sizeof(char)*num);
    std::cout << "变量p的值:"  << static_cast<void *>(p) << std::endl;
    std::cout << "变量p的地址:"  << &p << std::endl;
}


int main(int argc, char** argv) {

    int a = 1;
    int b = 2;
    char c = 'A';
    int *p = &a;
    int arr[] = {1,2,3,4,5};


    std::cout << "a=" << a <<", b=" << b << std::endl;
    std::cout << "c=" << c << std::endl;
    std::cout << "p=" << p << std::endl;
    std::cout << "*p=" << *p << std::endl;

    std::cout <<arr << std::endl;
    std::cout << std::endl << "************传值**************"<< std::endl;
    std::cout << "调用交换函数前: a=" << a <<", b=" << b << std::endl;
    exchg1(a, b);
    std::cout << "调用交换函数后: a=" << a <<", b=" << b << std::endl;

    std::cout << std::endl << "************传地址**************"<< std::endl;
    std::cout << "调用交换函数前: a=" << a <<", b=" << b << std::endl;
    exchg2(&a, &b);
    std::cout << "调用交换函数后: a=" << a <<", b=" << b << std::endl;


    std::cout << std::endl << "************传引用**************"<< std::endl;
    std::cout << "调用交换函数前: a=" << a <<", b=" << b << std::endl;
    exchg3(a, b);
    std::cout << "调用交换函数后: a=" << a <<", b=" << b << std::endl;

    std::cout << std::endl<< "***************************************"<< std::endl;
    char *str = NULL;
    getMemory(str,100);
    std::cout << "变量str的值:"  << static_cast<void *>(str) << std::endl;
    std::cout << "变量str的地址:"  << &str << std::endl;
    return 0;
}
